<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="课程标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入课程标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!-- <el-form-item label="教师ID" prop="teacherId">
        <el-input
          v-model="queryParams.teacherId"
          placeholder="请输入教师ID"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="教师名称" prop="teacherName">
        <el-input
          v-model="queryParams.teacherName"
          placeholder="请输入教师名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程简介" prop="jianjie">
        <el-input
          v-model="queryParams.jianjie"
          placeholder="请输入课程简介"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建日期" prop="addTime">
        <el-date-picker clearable
          v-model="queryParams.addTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择创建日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="价格" prop="jiage">
        <el-input
          v-model="queryParams.jiage"
          placeholder="请输入价格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="会员价" prop="huiyuanjia">
        <el-input
          v-model="queryParams.huiyuanjia"
          placeholder="请输入会员价"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_upd_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="推荐" prop="tuijian">
        <el-select v-model="queryParams.tuijian" placeholder="请选择推荐" clearable>
          <el-option
            v-for="dict in dict.type.sys_tuijian"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ziliao:videoke:add']"
        >新增合集</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['ziliao:videoke:edit']"
        >修改合集</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['ziliao:videoke:remove']"
        >删除合集</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ziliao:videoke:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="videokeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合集ID" align="center" prop="id" />
      <el-table-column label="缩略图" align="center" prop="pic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.pic" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="课程标题" align="center" prop="title" />
      <!-- <el-table-column label="教师ID" align="center" prop="teacherId" />
      <el-table-column label="教师名称" align="center" prop="teacherName" />
      <el-table-column label="教师头像" align="center" prop="teacherPic" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.teacherPic" :width="50" :height="50"/>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="课程简介" align="center" prop="jianjie" /> -->
      <!-- <el-table-column label="创建日期" align="center" prop="addTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.addTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="年级" align="center" prop="nianji" />
      <el-table-column label="版本" align="center" prop="banben" />
      <el-table-column label="学科" align="center" prop="xueke" /> -->
      <el-table-column label="价格" align="center" prop="jiage" />
      <el-table-column label="会员价" align="center" prop="huiyuanjia" />
      <!-- <el-table-column label="上下册" align="center" prop="cetype" /> -->
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_upd_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="推荐" align="center" prop="tuijian">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_tuijian" :value="scope.row.tuijian"/>
        </template>
      </el-table-column>
      <el-table-column label="视频管理" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleVideoMgr(scope.row)"
            v-hasPermi="['ziliao:videoke:edit']"
          >视频管理</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ziliao:videoke:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['ziliao:videoke:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改视频课程对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入课程标题" />
        </el-form-item>
        <!-- <el-form-item label="教师ID" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师ID" />
        </el-form-item> -->
        <el-form-item label="合集图" prop="pic">
          <image-upload :limit="1" v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="课程简介" prop="jianjie">
          <el-input v-model="form.jianjie" placeholder="请输入课程简介" />
        </el-form-item>
        <el-form-item label="所属年级" prop="nianji">
          <el-select multiple allow-create v-model="form.nianji" placeholder="请选择年级">
            <el-option
              v-for="item in njs"
              :key="item.id+''"
              :label="item.name"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联学科" prop="xueke">
          <el-select multiple allow-create v-model="form.xueke" placeholder="请选择学科">
            <el-option
              v-for="item in kecheng"
              :key="item.id+''"
              :label="item.name"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联版本" prop="banben">
          <el-select multiple allow-create v-model="form.banben" placeholder="请选择版本">
            <el-option
              v-for="item in bbs"
              :key="item.id+''"
              :label="item.banben"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item label="关联学期" prop="cetype">
          <el-select multiple allow-create v-model="form.cetype" placeholder="请选择学期">
            <el-option
              v-for="item in cetypes"
              :key="item.id+''"
              :label="item.ceName"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="教师名称" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师名称" />
        </el-form-item>
        <el-form-item label="教师头像" prop="teacherPic">
          <image-upload :limit="1" v-model="form.teacherPic"/>
        </el-form-item>
        <!-- <el-form-item label="创建日期" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="价格" prop="jiage">
          <el-input v-model="form.jiage" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="会员价" prop="huiyuanjia">
          <el-input v-model="form.huiyuanjia" placeholder="请输入会员价" />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_upd_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推荐" prop="tuijian">
          <el-radio-group v-model="form.tuijian">
            <el-radio
              v-for="dict in dict.type.sys_tuijian"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>



    <!-- 视频上传列表 -->
    <el-dialog title="视频管理" :visible.sync="spopen" fullscreen append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="课程标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入课程标题" />
        </el-form-item>
        <!-- <el-form-item label="教师ID" prop="teacherId">
          <el-input v-model="form.teacherId" placeholder="请输入教师ID" />
        </el-form-item> -->
        <el-form-item label="合集图" prop="pic">
          <image-upload :limit="1" v-model="form.pic"/>
        </el-form-item>
        <el-form-item label="课程简介" prop="jianjie">
          <el-input v-model="form.jianjie" placeholder="请输入课程简介" />
        </el-form-item>
        <el-form-item label="所属年级" prop="nianji">
          <el-select multiple allow-create v-model="form.nianji" placeholder="请选择年级">
            <el-option
              v-for="item in njs"
              :key="item.id+''"
              :label="item.name"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联学科" prop="xueke">
          <el-select multiple allow-create v-model="form.xueke" placeholder="请选择学科">
            <el-option
              v-for="item in kecheng"
              :key="item.id+''"
              :label="item.name"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="关联版本" prop="banben">
          <el-select multiple allow-create v-model="form.banben" placeholder="请选择版本">
            <el-option
              v-for="item in bbs"
              :key="item.id+''"
              :label="item.banben"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>

        
        <el-form-item label="关联学期" prop="cetype">
          <el-select multiple allow-create v-model="form.cetype" placeholder="请选择学期">
            <el-option
              v-for="item in cetypes"
              :key="item.id+''"
              :label="item.ceName"
              :value="item.id+''">
            </el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="教师名称" prop="teacherName">
          <el-input v-model="form.teacherName" placeholder="请输入教师名称" />
        </el-form-item>
        <el-form-item label="教师头像" prop="teacherPic">
          <image-upload :limit="1" v-model="form.teacherPic"/>
        </el-form-item>
        <!-- <el-form-item label="创建日期" prop="addTime">
          <el-date-picker clearable
            v-model="form.addTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建日期">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="价格" prop="jiage">
          <el-input v-model="form.jiage" placeholder="请输入价格" />
        </el-form-item>
        <el-form-item label="会员价" prop="huiyuanjia">
          <el-input v-model="form.huiyuanjia" placeholder="请输入会员价" />
        </el-form-item>
        
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_upd_type"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="推荐" prop="tuijian">
          <el-radio-group v-model="form.tuijian">
            <el-radio
              v-for="dict in dict.type.sys_tuijian"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listVideoke, getVideoke, delVideoke, addVideoke, updateVideoke } from "@/api/ziliao/videoke";
import { listAllNj } from "@/api/ziliao/nianji";
import { listXuekeAll } from "@/api/ziliao/xueke";
import { listAll } from "@/api/ziliao/banben";
import { listCelevelAll } from "@/api/ziliao/celevel"
export default {
  name: "Videoke",
  dicts: ['sys_upd_type', 'sys_tuijian'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 视频课程表格数据
      videokeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      spopen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        teacherId: null,
        teacherName: null,
        teacherPic: null,
        jianjie: null,
        addTime: null,
        nianji: null,
        banben: null,
        xueke: null,
        jiage: null,
        huiyuanjia: null,
        cetype: null,
        pic: null,
        status: null,
        tuijian: null
      },
      // 表单参数
      form: {
        nianji:''
      },
      // 表单校验
      rules: {
      },
      njs:null,
      kecheng:[],
      bbs:[],
      cetypes:[]
    };
  },
  created() {
    this.getnianji()
    this.getkecheng()
    this.getbanben()
    this.getcetype()
    this.getList();
  },
  methods: {
    getbanben(){
      listAll().then(res => {
        this.bbs = res.data
      })
    },
    getcetype(){
      listCelevelAll().then(res => {
        this.cetypes = res.data
      })
    },
    getnianji(){
      listAllNj().then(res => {
        this.njs = res.data
      })
    },
    getkecheng(){
      listXuekeAll().then(res => {
        this.kecheng = res.data
      })
    },
    /** 查询视频课程列表 */
    getList() {
      this.loading = true;
      listVideoke(this.queryParams).then(response => {
        this.videokeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        teacherId: null,
        teacherName: null,
        teacherPic: null,
        jianjie: null,
        addTime: null,
        nianji: null,
        banben: null,
        xueke: null,
        jiage: null,
        huiyuanjia: null,
        cetype: null,
        pic: null,
        status: null,
        tuijian: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加视频课程";
    },
    handleVideoMgr(row){
      const id = row.id;
      this.$router.push("/ziliao/videoke/videoMgr/" + id);
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getVideoke(id).then(response => {
        var d = response.data
        d.nianji = d.nianji.split(',')
        d.banben = d.banben.split(',')
        d.xueke = d.xueke.split(',')
        d.cetype = d.cetype.split(',')
        this.form = d;
        this.open = true;
        this.title = "修改视频课程";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.nianji = this.form.nianji.join(',')
          this.form.banben = this.form.banben.join(',')
          this.form.xueke = this.form.xueke.join(',')
          this.form.cetype = this.form.cetype.join(',')
          if (this.form.id != null) {
            updateVideoke(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVideoke(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除视频课程编号为"' + ids + '"的数据项？').then(function() {
        return delVideoke(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ziliao/videoke/export', {
        ...this.queryParams
      }, `videoke_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100% !important;
}
</style>